﻿using System;
using System.Data;
using Team4Mock.Entity;

namespace Team4Mock.UserControl.Department
{
    public partial class ViewAll : System.Web.UI.UserControl
    {
        OrganisationEntity oe = new OrganisationEntity();
        DepartmentEntity de = new DepartmentEntity();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dlCountry.DataSource = oe.GetCountry();
                dlCountry.DataBind();
                if (!string.IsNullOrEmpty(Request.QueryString["D"]))
                {
                    try
                    {
                        int departmentid = int.Parse(Request.QueryString["D"]);
                        de.DepartmentID = departmentid;
                        DataTable dt = new DataTable();
                        dt = de.GetById(de);
                        if (dt.Rows.Count > 0)
                        {
                            txtPartName.Text = dt.Rows[0][1].ToString();
                            txtDesShort.Text = dt.Rows[0][2].ToString();
                            idContact.Value = dt.Rows[0][3].ToString();
                            txtAdd1.Text = dt.Rows[0][4].ToString();
                            txtAdd2.Text = dt.Rows[0][5].ToString();
                            txtAdd3.Text = dt.Rows[0][6].ToString();
                            PostCode.Value = dt.Rows[0][7].ToString();
                            Town.Value = dt.Rows[0][8].ToString();
                            County.Value = dt.Rows[0][9].ToString();
                            dlCountry.SelectedValue = dt.Rows[0][10].ToString();
                            hdidBus.Value = dt.Rows[0][11].ToString();
                            hdsic.Value = dt.Rows[0][12].ToString();
                            txtDesFull.Text = dt.Rows[0][13].ToString();
                            txtPhone.Text = dt.Rows[0][14].ToString();
                            txtFax.Text = dt.Rows[0][15].ToString();
                            txtWeb.Text = dt.Rows[0][16].ToString();
                            txtCharNum.Text = dt.Rows[0][17].ToString();
                            txtComNum.Text = dt.Rows[0][18].ToString();
                            if (dt.Rows[0][20].ToString() == "1")
                            {
                                ckIsActive.Checked = true;
                            }
                            else
                            {
                                ckIsActive.Checked = false;
                            }
                        }
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("/FormView/Directorate.aspx?s=" + Request.QueryString["s"] + "&ID=" + Request.QueryString["ID"]);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtPartName.Text == "" || idContact.Value=="" || hdidBus.Value=="")
            {
                tb.Text = "Input Department Name and Select Lead Contact, Type of Business";
            }
            else
            {
                if (rdOrg.Checked == true || rdParent.Checked == true)
                {
                    Copy();
                }
                else
                {
                    if (txtAdd1.Text == "" || PostCode.Value == "")
                    {
                        tb.Text = "Input Address Line 1 and Select Post Code";
                    }
                    else
                    {
                        de.DepartmentName = txtPartName.Text;
                        de.DepartmentShortDescription = txtDesShort.Text;
                        de.ContactID = int.Parse(idContact.Value);
                        de.AddressLine1 = txtAdd1.Text;
                        de.AddressLine2 = txtAdd2.Text;
                        de.AddressLine3 = txtAdd3.Text;
                        de.Postcode = PostCode.Value;
                        de.Town = Town.Value;
                        de.County = County.Value;
                        de.Country = dlCountry.SelectedValue;
                        de.TypeOfBusiness = hdidBus.Value;
                        de.SICCode = hdsic.Value;
                        de.DepartmentFullDescription = txtDesFull.Text;
                        de.PhoneNumber = txtPhone.Text;
                        de.Fax = txtFax.Text;
                        de.WebAddress = txtWeb.Text;
                        de.CharityNumber = txtCharNum.Text;
                        de.CompanyNumber = txtComNum.Text;
                        de.IsActive = ckIsActive.Checked ? 1 : 0;

                       
                    }
                   
                }
                //Create
                if (string.IsNullOrEmpty(Request.QueryString["D"]))
                {
                    try
                    {

                        de.DirectorateID = int.Parse(Request.QueryString["s"]);
                        de.Insert(de);
                        tb.Text = "Insert Success";
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
                //Amend
                else
                {
                    try
                    {
                        de.DepartmentID = int.Parse(Request.QueryString["D"]);
                        de.Update(de);
                        tb.Text = "Update Success";
                    }
                    catch
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
                    }
                }
                
            }

        }
        void Copy()
        {
            string name = txtPartName.Text, shortdes = txtDesShort.Text, bus = hdidBus.Value, sic = hdsic.Value, contactid = idContact.Value, full = txtDesFull.Text, orgid = Request.QueryString["ID"], direcid = Request.QueryString["s"];
            string add1 = "", add2 = "", add3 = "", postcode = "", town = "", county = "", country = "", phone = "", fax = "", web = "", charity = "", company = "";
            try
            {
                int idorg = int.Parse(orgid);
                int iddirec = int.Parse(direcid);

                DataTable dt = new DataTable();
                if (rdOrg.Checked == true)
                {
                    de.OrganisationID = idorg;
                    dt = de.CopyOrg(de);
                }
                if (rdParent.Checked == true)
                {
                    de.DirectorateID = iddirec;
                    dt = de.CopyDirec(de);
                }
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    add1 = dt.Rows[0][0].ToString();
                    add2 = dt.Rows[0][1].ToString();
                    add3 = dt.Rows[0][2].ToString();
                    postcode = dt.Rows[0][3].ToString();
                    town = dt.Rows[0][4].ToString();
                    county = dt.Rows[0][5].ToString();
                    country = dt.Rows[0][6].ToString();
                    phone = dt.Rows[0][7].ToString();
                    fax = dt.Rows[0][8].ToString();
                    web = dt.Rows[0][9].ToString();
                    charity = dt.Rows[0][10].ToString();
                    company = dt.Rows[0][11].ToString();
                }
                de.DepartmentName = name;
                de.DepartmentShortDescription = shortdes;
                de.ContactID = int.Parse(contactid);
                de.AddressLine1 = add1;
                de.AddressLine2 = add2;
                de.AddressLine3 = add3;
                de.Postcode = postcode;
                de.Town = town;
                de.County = county;
                de.Country = country;
                de.TypeOfBusiness = bus;
                de.SICCode = sic;
                de.DepartmentFullDescription = full;
                de.PhoneNumber = phone;
                de.Fax = fax;
                de.WebAddress = web;
                de.CharityNumber = charity;
                de.CompanyNumber = company;
                de.IsActive = ckIsActive.Checked ? 1 : 0;
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "Error();", true);
            }
        }
    }
}